import curses

from shared.colors import Color


class PageBar:
	"""Page indicator bar."""

	def __init__(self, parent, window):
		self._parent = parent
		self._window = window
		self._current_page = 1

		# configure window
		self._window.bkgd(curses.color_pair(Color.PAGE_BAR))
		self._draw_bar()

	def _draw_bar(self):
		"""Draw pages on the bar"""
		pos_x = 0

		for index, page in enumerate(self._parent._pages, 1):
			page_title = ('  {0}  ', '[ {0} ]')[index == self._current_page].format(page)
			self._window.addstr(0, pos_x, page_title)
			pos_x += len(page_title)

		self._window.noutrefresh()

	def notify_page_change(self, page):
		"""Notification from main thread that page has been changed"""
		self._current_page = page
		self._draw_bar()
